// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_disabled: false,
    username: "",
    password: "",
  },

  signup: function () {
    wx.navigateTo({
      url: '/pages/enroll/enroll'
    })
  },
  test: function(){
    wx.openSetting({
      success (res) {
        console.log(res.authSetting)
        // res.authSetting = {
        //   "scope.userInfo": true,
        //   "scope.userLocation": true
        // }
      }
    })
    console.log("测试");
    console.log(getApp().globalData.userInfo)
  },

  login: function (e) {
    var that = this
    if (that.data.username == '') {
      wx.showModal({   //展示弹窗方法
        title: '提示！',
        showCancel: false,
        content: '请输入用户名！',    //内容
        success: function (res) { }
      })
    }
    else if (that.data.password == '') {
      wx.showModal({
        title: '提示！',
        showCancel: false,
        content: '请输入密码！',
        success: function (res) { }
      })
    }
    else {
      console.log("success")
      console.log(that.data.username)
      wx.request({
        url: getApp().globalData.server+'/User/login',
        data: {
          phone:that.data.username,
          password:that.data.password
        },
        method:"POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded' 
        },
        success (res) {
          console.log(res.data)
          if(res.data.error_code==1)
          {
            wx.showModal({
              title: '提示！',
              content: res.data.msg,
              showCancel: false,
              success(res) {}
            })
          }
          else if(res.data.error_code==2)
          {
            wx.showModal({
              title: '提示！',
              content: res.data.msg,
              showCancel: false,
              success(res) {}
            })
          }
          else if(res.data.error_code==3)
          {
            wx.showModal({
              title: '提示！',
              content: res.data.msg,
              showCancel: false,
              success(res) {}
            })
          }
          else if(res.data.error_code!=0)
          {
            wx.showModal({
              title: '提示！',
              content: '其他错误',
              showCancel: false,
              success(res) {}
            })
          }
          else if(res.data.error_code==0)
          {
            getApp().globalData.userid=res.data.data.user_id
            console.log(getApp().globalData.userid)
            getApp().globalData.user=res.data.data
            wx.showModal({
              title: '提示！',
              content: '成功！',
              showCancel: false,
              success(res) {},
              complete:function(res){
                wx.reLaunch({
                  url: '/pages/square/square',
                })
              }
            })
          }
        },
        fail:function(res)
        {
          wx.showModal({
            title: '提示！',
            content: 'Fail Connect!',
            showCancel: false,
            success(res) {}
          })
        }
      })
    }
  },

  //像不像面向对象？？
  usernameInput: function (e) {
    this.data.username = e.detail.value
  },

  passwordInput: function (e) {
    this.data.password = e.detail.value
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})